//
//  FirstViewController.m
//  Sultry
//
//  Created by zhouliangfei on 2016/10/31.
//  Copyright © 2016年 zhouliangfei. All rights reserved.
//
#import "TMPlug.h"
#import "TMLoader.h"
#import "FirstViewController.h"

@implementation FirstViewController

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //
    [TMPlug loadLibrary:@[@"http://localhost/sultry/firstCtrl.framework.zip"] complete:^(id plug) {
        NSBundle *firstCtrl = [TMPlug regeditPlug:plug];
        if(firstCtrl){
            Class initMain = [firstCtrl classNamed:@"initMain"];
            if (initMain) {
                NSObject *pacteraObject = [initMain new];
                [pacteraObject performSelector:@selector(initMain:bundle:) withObject:self withObject:firstCtrl];
            }
            [firstCtrl unload];
        }
    }];
    //
    UIImageView *v = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    [self.view addSubview:v];
    //
    [v load:@"uploads/blog/201409/20/20140920153536_8WsvT.jpeg" base:@"http://img3.duitang.com/"];
    //
    return;
    
    //
    // 定义读写的 stream.
    NSInputStream *inStream = nil;
    NSOutputStream *outStream = nil;
    [NSStream getStreamsToHostWithName:@"localhost" port:1883 inputStream:&inStream outputStream:&outStream];
    //
    if (inStream && outStream) {
        [self setInStream:inStream];
        [self.inStream setDelegate:self];
        [self.inStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
        [self.inStream open];
        //
        [self setOutStream:outStream];
        [self.outStream setDelegate:self];
        [self.outStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
        [self.outStream open];
        //
        [self performSelector:@selector(sendMessage:) withObject:@"xxxx" afterDelay:10];
    }
}
- (void)sendMessage:(NSString*)message{
    if(message.length > 0){
        [self.outStream write:(const uint8_t*)[message UTF8String] maxLength:message.length];
    }
}
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode{
    switch (eventCode) {
            //发送
        case NSStreamEventHasSpaceAvailable:{
            NSLog(@"发送成功!");
            break;
        }
            //接收
        case NSStreamEventHasBytesAvailable: {
            if (_receivedData == nil) {
                _receivedData = [[NSMutableData alloc] init];
            }
            
            uint8_t buf[1024];
            NSInteger numBytesRead = [(NSInputStream *)aStream read:buf maxLength:1024];
            
            if (numBytesRead > 0) {
                [self didReceiveData:[NSData dataWithBytes:buf length:numBytesRead]];
                
            } else if (numBytesRead == 0) {
                NSLog(@" >> End of stream reached");
            } else {
                NSLog(@" >> Read error occurred");
            }
            break;
        }
            //完成
        case NSStreamEventEndEncountered: {
            
            [self cleanUpStream:aStream];
            
            [self didFinishReceivingData];
            
            break;
        }
            //打开
        case NSStreamEventOpenCompleted:{
            //NSLog(@"%@",aStream);
            //
            //if(aStream == self.outStream){
                //[self sendData:[NSData dataWithBytes:"@" length:1]];
            //}
        }
            //错误
        case NSStreamEventErrorOccurred: {
            NSError * error = [aStream streamError];
            NSString * errorInfo = [NSString stringWithFormat:@"Failed while reading stream; error '%@' (code %ld)", error.localizedDescription, (long)error.code];
            
            [self cleanUpStream:aStream];
            
            [self networkFailedWithErrorMessage:errorInfo];
        }
        default:
            break;
    }
}
- (void)networkFailedWithErrorMessage:(NSString*)sender{
    
}
- (void)cleanUpStream:(NSStream *)stream{
    [stream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [stream close];
    stream = nil;
}
- (void)didReceiveData:(NSData*)data{
    
}
- (void)didFinishReceivingData{
    
}
-(void)sendData:(NSData*)data{
    //[self.outStream write:data.bytes maxLength:data.length];
}
@end
